﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace Api.Model
{
    [Table("method")]
    public class Method : BaseUserModel
    {
        public Method()
        {
            Parameters = new List<MethodParameter>();
        }
        public int ProjectId { get; set; }
        public int CategoryId { get; set; }
        [MaxLength(1000)]
        public string Name { get; set; }
        /// <summary>
        /// 接口地址
        /// http://op.juhe.cn/trainTickets/cityCode
        /// </summary>
        [MaxLength(1000)]
        public string Url { get; set; }

        /// <summary>
        /// 支持格式
        /// json/xml/html
        /// </summary>
        [MaxLength(50)]
        public string SupportFomats { get; set; } = "json";

        /// <summary>
        /// 请求方式：
        /// http get/post
        /// </summary>
        public MethodType HttpMethod { get; set; } = MethodType.Get;
        public bool IsNeedLogin { get; set; } = false;

        /// <summary>
        /// 是否访问次数限制
        /// </summary>
        public bool IsTimeLimit { get; set; } = false;
        [MaxLength(50)]
        public string RequestLevel { get; set; }

        /// <summary>
        /// 借口备注：
        /// </summary>
        [Column(TypeName = "text")]
        [AllowHtml]
        public string Description { get; set; }
        /// <summary>
        /// JSON返回示例：
        /// </summary>
        [Column(TypeName = "text")]
        [AllowHtml]
        public string ResultDemo { get; set; }
        /// <summary>
        /// 返回值说明
        /// </summary>
        [Column(TypeName = "text")]
        [AllowHtml]
        public string ResultDesc { get; set; }

        public int UserId { get; set; }

        /// <summary>
        /// 请求参数说明：
        /// </summary>
        [NotMapped]
        public List<MethodParameter> Parameters { get; set; }
        [NotMapped]
        public string DescriptionIgnorHtml
        {
            get
            {
                var description = Description ?? "";
                return Regex.Replace(description, "<[^>]*>", "").Replace(" ", "").Replace("\n", "").Replace("\t", "").Replace("\r", "");
            }
        }
    }


    public enum ParameterType
    {
        Query = 1,
        Return = 2
    }

    public enum MethodType
    {
        Get = 0,
        Post = 1
    }
}
